package init;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.SwingConstants;

import model.Game;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import client.Listener;

public class GameFrame extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField txtPort;
	private JTextField txtCoinsNumber;
	private JTextField txtNick;
	private JTextField txtSmallBlind;
	private JTextField txtBigBlind;
	private JLabel lblNick;
	private JButton btnAction;
	private JComboBox<String> comboBox;
	private static int MAX_NAME_SIZE = 15;

public GameFrame()
	{
		setBounds(100, 100, 440, 302);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNrPortu = new JLabel("Nr Portu");
		lblNrPortu.setBounds(28, 15, 108, 20);
		lblNrPortu.setHorizontalAlignment(SwingConstants.RIGHT);
		contentPane.add(lblNrPortu);
		
		txtPort = new JTextField(4);
		txtPort.setText("4444");
		txtPort.setBounds(146, 15, 190, 20);
		contentPane.add(txtPort);
		txtPort.setColumns(4);
		
		JLabel lblLiczbaGraczy = new JLabel("Number of Players");
		lblLiczbaGraczy.setBounds(28, 50, 108, 20);
		lblLiczbaGraczy.setHorizontalAlignment(SwingConstants.RIGHT);
		contentPane.add(lblLiczbaGraczy);
			
		comboBox = new JComboBox<String>();
		comboBox.setModel(new DefaultComboBoxModel<String>(new String[] {"2", "3", "4", "5", "6"}));
		comboBox.setBounds(146, 50, 42, 20);
		contentPane.add(comboBox);
			
		JLabel lblCoinsNumber = new JLabel("Number of Coins");
		lblCoinsNumber.setBounds(28, 85, 108, 20);
		lblCoinsNumber.setHorizontalAlignment(SwingConstants.RIGHT);
		contentPane.add(lblCoinsNumber);
			
		txtCoinsNumber = new JTextField("10000");
		txtCoinsNumber.setBounds(146, 85, 190, 20);
		contentPane.add(txtCoinsNumber);
		txtCoinsNumber.setColumns(10);
		
		JLabel lblSmallBlind = new JLabel("Small Blind");
		lblSmallBlind.setBounds(28, 120, 108, 20);
		lblSmallBlind.setHorizontalAlignment(SwingConstants.RIGHT);
		contentPane.add(lblSmallBlind);
			
		txtSmallBlind = new JTextField("20");
		txtSmallBlind.setBounds(146, 120, 66, 20);
		contentPane.add(txtSmallBlind);
		txtSmallBlind.setColumns(10);
		
		JLabel lblBigBlind = new JLabel("Big Blind");
		lblBigBlind.setBounds(203, 120, 60, 20);
		lblBigBlind.setHorizontalAlignment(SwingConstants.RIGHT);
		contentPane.add(lblBigBlind);
			
		txtBigBlind = new JTextField("30");
		txtBigBlind.setBounds(270, 120, 66, 20);
		contentPane.add(txtBigBlind);
		txtBigBlind.setColumns(10);
		
		lblNick = new JLabel("Your Nick");
		lblNick.setBounds(28, 155, 108, 20);
		lblNick.setHorizontalAlignment(SwingConstants.RIGHT);
		contentPane.add(lblNick);
		
		txtNick = new JTextField(MAX_NAME_SIZE);
		txtNick.setBounds(146, 155, 190, 20);
		contentPane.add(txtNick);
		txtNick.setColumns(10);
		
		btnAction = new JButton("Create");
		btnAction.addActionListener(this);
		
		btnAction.setBounds(146, 193, 190, 23);
		contentPane.add(btnAction);
	}
	public void actionPerformed(ActionEvent event)
	{
		if (event.getActionCommand()=="Create" )
		{
			try
			{   
				int port = Integer.parseInt(txtPort.getText());
				int players  = Integer.parseInt((String)comboBox.getSelectedItem());
				int coins = Integer.parseInt(txtCoinsNumber.getText());
				int smallBlind = Integer.parseInt(txtSmallBlind.getText());
				System.out.println(smallBlind);
				int bigBlind = Integer.parseInt(txtBigBlind.getText());
				String name = txtNick.getText();
				while(name.length()<15){
					name = name+" ";
				}
				if (name.length()>15) name = name.substring(0,15);
				Game game = new Game(players,coins,port,name,smallBlind,bigBlind);
				port = Integer.parseInt(txtPort.getText());
				Listener t = new Listener(port,"localhost",name);
				t.start();
				dispose();
			} 
			catch (NumberFormatException e)
			{
				String error = e.getMessage().substring(16);
				JOptionPane.showMessageDialog(null, "Incorrect data" + error,
						"Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}	
}
